#include "plant.h"


plant::plant(int T,QString N,int R,int C)
{
    type=T;
    name=N;
    row=R;
    col=C;
    if(type==1 ||type==2 ||type==3)
    {
        init_life=life=500;
    }
    if(name=="Nut")
    {
        init_life=life=4000;
        sum_fps=5;
        path=":/nut/";
    }
    if(type==1)
    {
        if(name=="Single_Pea")
        {
            bullet_type=0;
            attack=100;
            sum_fps=8;
            path=":/Peashooter/Peashooter";
        }
        else if(name=="Snow_Pea")
        {
            bullet_type=1;
            attack=100;
            sum_fps=8;
            path=":/SnowPea/SnowPea";
        }
    }
    if(type==2)
    {
        if(name=="Potato_Mine")
        {
            attack=5000;
            sum_fps=8;
            path=":/PotatoMine/PotatoMine";
        }

    }
    if(type==3)
    {
        if(name=="Sun_Flower")
        {
            sum_fps=8;
            path=":/SunFlower/SunFlower";
        }
    }

}

void plant::update(QVector<zom>& z,QVector<sunshine>&sunshines,QPainter* pa,QPixmap& pm)
{

    int x=330+col*105+50;
    int y=170+row*210+55;
    fps++;
    if(fps>sum_fps)
        fps=1;
    if(type==1)
    {
        if(!z.empty()&&z.back().life>0)
        {

            if(bullet_pre>=bullet_fre)
            {
                bullet_pre=0;
                Bullets.push_back(new bullet(x+33,y,bullet_type,bullet_speed,attack));
            }
            else
                bullet_pre+=bullet_re;
        }

        auto it=Bullets.begin();
        while(it!=Bullets.end())
        {
            int attack=(*it)->attack;
            int x=(*it)->x-60;  //方便判定植物离僵尸很近能打到僵尸
            int y=(*it)->y;
            int type=(*it)->type;
            int w=50;
            QString str=":/bullet/";
            if(type==1)
            {
                str+="SnowBullet.png";
                w+=20;
            }
            else if(type==0)
                str+="PeaNormal_0.png";
            pm.load(str);
            pa->drawPixmap(x+60,y,w,50,pm);
            if(x>=1300)
            {
                it=Bullets.erase(it);
                continue;
            }
            if( !z.empty()&&z.back().life>0)
            {

                int pos=0;
                while(z[pos].life<=0)
                    pos++;
                zom& z1=z[pos];
                if(abs(x-z1.x)<=40)
                {

                    z1.life-=attack;
                    if(type==1&&z1.reduce_speed==false)
                    {
                        z1.reduce_speed_pre=0;
                        z1.reduce_speed=true;
                        z1.walk_speed/=2;
                        z1.eat_fre*=2;
                    }
                    it=Bullets.erase(it);
                    continue;
                }
            }
            (*it)->RenewPos();
            ++it;
        }
    }
    else if(type==2)
    {
        if(name=="Potato_Mine")
        {
            if(statue==0)
            {
                path=":/PotatoMine/potato_prepare.png";
                fps=0;
                if(boom_pre>=boom_fre)
                    statue=1;
                else
                    boom_pre+=boom_re;
            }
            else if(statue==1)
            {
                if(able_boom==false)
                {
                    fps=1;
                    able_boom=true;
                }
                path=":/PotatoMine/PotatoMine";
            }
            else if(statue==2)
            {
                fps=0;
                path=":/PotatoMine/PotatoMine_mashed.png";
            }
        }

    }
    else if(type==3)
    {
        if(sun_pre>=sun_fre)
        {
            sun_pre=0;
            sunshines.push_back(sunshine(sun_pre_num,x,y));
        }
        else
            sun_pre+=sun_re;
    }
}
